package hotciv.view;

import hotciv.framework.Game;

import java.awt.event.MouseEvent;

import minidraw.framework.Drawing;
import minidraw.framework.DrawingEditor;
import minidraw.framework.Figure;
import minidraw.standard.NullTool;

public class EndTurnTool extends NullTool {

	// We wanna get and remember the editor and the game instances, please!
	private final DrawingEditor editor;
	private final Game game;
	
	// Determines, wether the mouse is pressed down on the shield
	private boolean mouseDownOnShield = false;
	
	public EndTurnTool (DrawingEditor editor, Game game) {
		this.game = game;
		this.editor = editor;
	}
	
	@Override
	public void mouseDown(MouseEvent event, int x, int y) {
		mouseDownOnShield = isShieldFigureAt(x, y);
	}

	@Override
	public void mouseUp(MouseEvent event, int x, int y) {
		// Check if the mouse was pressed and released on the shield icon
		if (mouseDownOnShield && isShieldFigureAt(x, y)) {
			game.endOfTurn();
		}
	}
	
	private boolean isShieldFigureAt(int x, int y) {
		Drawing d = editor.drawing();
		// The figure we test
		Figure testingFigure = d.findFigure(x, y);
		// The shield figure
		Figure shieldFigure = d.findFigure(GfxConstants.TURN_SHIELD_X, GfxConstants.TURN_SHIELD_Y);
		// The testing figure and the shield figure should be the exact same, and the testing figure should not be null (to not return true, if there are no shield figure)
		return (testingFigure != null && testingFigure == shieldFigure);
	}

}
